import random
from pynput import keyboard
from playsound import playsound
from threading import Thread
import time
count = 1
begin=time.perf_counter()
prev=1

def onRelease(key):
    # print(key)  ##回调函数，Listener自动调用
    global count
    global prev
    global begin
    #if 'a'<= str(key)[1] <='z' or '0'<= str(key)[1] <='9' :
    count += 1
    end=time.perf_counter()
    cur=count
    print((cur-prev+1)/(end-begin+1))
    print(count)
    if count%15 ==0:
        if (cur-prev+1)/(end-begin+1) >= 9:
            s = "sound/"
            a =random.randint(9,12)
            s += str(a) + ".mp3"
            print(s)
            # 创建线程避免卡顿
            # #playsound(s)
            t = Thread(target=playsound,args=(s,))
            t.start()
        elif (cur-prev+1)/(end-begin+1) >= 6:
            s = "sound/"
            a = random.randint(5, 8)
            s += str(a) + ".mp3"
            print(s)
            # 创建线程避免卡顿
            # #playsound(s)
            t = Thread(target=playsound, args=(s,))
            t.start()
        elif (cur-prev+1)/(end-begin+1) >= 3:
            s = "sound/"
            a = random.randint(1, 4)
            s += str(a) + ".mp3"
            print(s)
            # 创建线程避免卡顿
            # #playsound(s)
            t = Thread(target=playsound, args=(s,))
            t.start()
        begin = time.perf_counter()
        prev = cur

print("程序已运行》》》》》》》》》》》》》》》》》》》》")
time.sleep(5)
listener = keyboard.Listener(on_release=onRelease)
listener.start()
listener.join()


#
